﻿/*
 * @(#)StTool.cs, 2010-5-4 17:20:04.
 *
 * @author fengxianwen.
 *
 *
 */
using System;
using System.Text;
using System.Security.Cryptography;
using JKF.Substructure.Properties;
//using Substructure;
namespace JKF.Substructure
{
    /// <summary>
    /// 静态工具。
    /// </summary>
    public static class StTool
    {
        /// <summary>
        /// 计算组织机构代码的校验码。
        /// </summary>
        /// <param name="orgCode8">8位组织机构代码本体码</param>
        public static char GetOrgCheckCode(string orgCode8)
        {
            if (string.IsNullOrEmpty(orgCode8) || orgCode8.Length != 8)
            {
                throw new ApplicationException("组织机构本体码必须是 8 位。");
            }

            // 加权码
            int[] w = { 3, 7, 9, 10, 5, 8, 4, 2 };

            int sum = 0;

            for (int i = 0; i < 8; i++)
            {
                int c = orgCode8[i];

                if (c < 48 || c > 90 || (c > 57 && c < 65))
                {
                    throw new ApplicationException("组织机构本体码必须由数字和大写拉丁字母组成。");
                }

                c -= c < 65 ? 48 : 55;

                sum += w[i] * c;
            }

            sum = 11 - sum % 11;

            return sum < 10 ? sum.ToString()[0] : sum > 10 ? '0' : 'X';
        }

        /// <summary>
        /// 生成给定字符串的MD5串。
        /// </summary>
        public static string MD5(string src)
        {
            return MD5(Encoding.Default.GetBytes(src));
        }

        /// <summary>
        /// 生成给定字节数组的MD5串。
        /// </summary>
        public static string MD5(byte[] src)
        {
            if (src == null) return null;

            byte[] b = new MD5CryptoServiceProvider().ComputeHash(src);

            return GetHexStr4Bytes(b);
        }

        /// <summary>
        /// 获取指定字节数组的十六进制表示。
        /// </summary>
        public static string GetHexStr4Bytes(byte[] b)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < b.Length; i++)
            {
                sb.Append(b[i].ToString("x").PadLeft(2, '0'));
            }

            return sb.ToString();
        }

        /// <summary>
        /// 转换 byte[16] 为 Oracle 字符串表示。
        /// </summary>
        public static string Guid2OraString(byte[] b)
        {
            if (b == null || b.Length != 16)
            {
                throw new ApplicationException("字节数组长度必须是16。");
            }

            byte[] bt = new byte[] 
            {
                b[3],b[2],b[1],b[0],
                b[5],b[4],
                b[7],b[6],
                b[8],b[9],
                b[10],b[11],b[12],b[13],b[14],b[15]
            };

            return new Guid(bt).ToString("N").ToUpper();
        }

        /// <summary>
        /// 转换 Guid 为 Oracle 字符串表示。
        /// </summary>
        public static string Guid2OraString(Guid g)
        {
            return Guid2OraString(g.ToByteArray());
        }

        /// <summary>
        /// 转换 C# 字符串表述的 Guid 为 Oracle 字符串表示。
        /// </summary>
        public static string Guid2OraString(string s)
        {
            return Guid2OraString(new Guid(s).ToByteArray());
        }

        /// <summary>
        /// 转换 Oracle 字符串表示的 GUID 为 C# Guid。
        /// </summary>
        public static Guid GuidFromOraString(string s)
        {
            return new Guid(Guid2OraString(new Guid(s).ToByteArray()));
        }

        /// <summary>
        /// 获取指定字符串的指定拼写类型的首字母串。
        /// </summary>
        public static string GetCnInitial(string text, CnSpellType t)
        {
            if (String.IsNullOrEmpty(text))
            {
                return text;
            }

            StringBuilder rt = new StringBuilder();

            for (int i = 0; i < text.Length; i++)
            {
                rt.Append(GetCnInitial(text[i], t));
            }

            return rt.ToString();
        }

        private static string[] pyx = new string[] { "\u3007", "l" };
        private static string[] wbx = new string[] { "\u3007\uE815\uE816\uE817\uE818\uE819\uE81A\uE81B\uE81C\uE81D\uE81E\uE81F\uE820\uE821\uE822\uE823\uE824\uE825\uE826\uE827\uE828\uE829\uE82A\uE82B\uE82C\uE82D\uE82E\uE82F\uE830\uE831\uE832\uE833\uE834\uE835\uE836\uE837\uE838\uE839\uE83A\uE83B\uE83C\uE83D\uE83E\uE83F\uE840\uE841\uE842\uE843\uE844\uE845\uE846\uE847\uE848\uE849\uE84A\uE84B\uE84C\uE84D\uE84E\uE84F\uE850\uE851\uE852\uE853\uE854\uE855\uE856\uE857\uE858\uE859\uE85A\uE85B\uE85C\uE85D\uE85E\uE85F\uE860\uE861\uE862\uE863\uE864\uF92C\uF979\uF995\uF9E7\uF9F1\uFA0C\uFA0D\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA18\uFA1F\uFA20\uFA21\uFA23\uFA24\uFA27\uFA28\uFA29", "lrdtnnwwqbckkkinnnrrrrrgasgitdinhttxpuufddveappuyymmkqqqqqqquuuufohqqqhegugghrndxyutybgfyfmssfaajfeqqb" };

        /// <summary>
        /// 获取指定字符的指定拼写类型的首字母。
        /// </summary>
        public static char GetCnInitial(char c, CnSpellType t)
        {
            int idx = (int)c - 19968;

            string s = "";
            string[] ss = null;

            switch (t)
            {
                case CnSpellType.Py:
                    s = Resources.PyInitials;
                    ss = pyx;
                    break;
                case CnSpellType.Wb:
                    s = Resources.WbInitials;
                    ss = wbx;
                    break;
                default:
                    break;
            }

            if (idx >= 0 && idx < s.Length)
            {
                return s[idx];
            }
            else
            {
                idx = ss[0].IndexOf(c);
                if (idx >= 0) return ss[1][idx];

                return char.ToLower(c);
            }
        }


        /**
	     * 
	     * 修补15位居民身份证号码为18位
	     * 
	     * @param personIDCode
	     * 
	     * @return
	     */
        public static string fixPersonIDCode(string personIDCode)
        {

            string retIDCode = "";

            if (personIDCode == null || personIDCode.Trim().Length != 15)
            {

                return personIDCode;

            }

            string id17 = personIDCode.Substring(0, 6) + "19"
                    + personIDCode.Substring(6); // 15位身份证补\'19\'

            char[] code = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; // 11个

            int[] factor = { 0, 2, 4, 8, 5, 10, 9, 7, 3, 6, 1, 2, 4, 8, 5, 10, 9, 7 }; // 18个;

            int[] idcd = new int[18];

            int i;

            int j;

            int sum;

            int remainder;

            for (i = 1; i < 18; i++)
            {

                j = 17 - i;

                idcd[i] = int.Parse(id17.Substring(j, 1));

            }

            sum = 0;

            for (i = 1; i < 18; i++)
            {

                sum = sum + idcd[i] * factor[i];

            }

            remainder = sum % 11;

            string lastCheckBit =code[remainder].ToString();

            return id17 + lastCheckBit;

        }


    }

    public enum CnSpellType
    {
        /// <summary>
        /// 拼音。
        /// </summary>
        Py,

        /// <summary>
        /// 五笔。
        /// </summary>
        Wb,
    }



}
